package com.duobaoyu.dby.supply.biz.event.listener;

import com.duobaoyu.dby.saas.product.common.remote.dto.SkuStockOrderNumDTO;
import com.duobaoyu.dby.supply.biz.client.product.ProductCenterApiClient;
import com.duobaoyu.dby.supply.biz.client.product.converter.ProductConverter;
import com.duobaoyu.dby.supply.biz.constant.SupplyChainConstants;
import com.duobaoyu.dby.supply.biz.event.SalesVolumeStatistics;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class SalesVolumeStatisticsListener implements ApplicationListener<SalesVolumeStatistics> {

	private final ProductConverter productConverter;
	private final ProductCenterApiClient productCenterApiClient;

	@Override
	@Async(SupplyChainConstants.THREAD_POOL_NAME)
	public void onApplicationEvent(SalesVolumeStatistics event) {
		//此操作是增量操作,需要进行埋点处理
		List<SalesVolumeStatistics.OrderSalesVolumeStatistics> orderSalesVolumeStatisticsList = event.getOrderSalesVolumeStatisticsList();
		List<SkuStockOrderNumDTO> stockOrderNumList = productConverter.toSkuStockOrderNum(orderSalesVolumeStatisticsList);
		productCenterApiClient.batchOrderNumUpdate(stockOrderNumList);
	}
}
